package com.wmuj.aspect;

import com.wmuj.annotation.LogOperation;
import com.wmuj.mapper.OperateLogMapper;
import com.wmuj.pojo.OperateLog;
import com.wmuj.utils.CurrentHolder;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.util.Arrays;

@Aspect
@Component
public class OperationLogAspect {

  @Autowired private OperateLogMapper operateLogMapper;

  // 环绕通知
  @Around("@annotation(log)")
  public Object around(ProceedingJoinPoint joinPoint, LogOperation log) throws Throwable {
    // 记录开始时间
    long startTime = System.currentTimeMillis();
    // 执行方法
    Object result = joinPoint.proceed();
    // 当前时间
    long endTime = System.currentTimeMillis();
    // 耗时
    long costTime = endTime - startTime;

    // 构建日志对象
    OperateLog operateLog = new OperateLog();
    operateLog.setOperateEmpId(getCurrentUserId()); // 需要实现 getCurrentUserId 方法
    operateLog.setOperateTime(LocalDateTime.now());
    operateLog.setClassName(joinPoint.getTarget().getClass().getName());
    operateLog.setMethodName(joinPoint.getSignature().getName());
    operateLog.setMethodParams(Arrays.toString(joinPoint.getArgs()));
    operateLog.setReturnValue(result.toString());
    operateLog.setCostTime(costTime);

    // 插入日志
    operateLogMapper.insert(operateLog);
    return result;
  }

  // 示例方法，获取当前用户ID
  private int getCurrentUserId() {
    // 获取员工的id的id返回
    return CurrentHolder.getCurrentId();
  }
}
